//
//  CancelViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/29.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "CancelViewController.h"
#import "ReadyDiSignle.h"
#import "Base.h"
#import "Targets.h"
#import "DetailViewController.h"
#import "OrderMessagesignle.h"
@interface CancelViewController ()
{
    UIScrollView *_scroller;
}
@property(nonatomic,strong)UIButton *itemButton;
@end

@implementation CancelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.view.backgroundColor=COLOR;
        }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"cancelArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
     _scroller.frame=CGRectMake(0, 0, WIDTH, HEIGHT-64);
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    self.title=@"已取消订单";
    
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"cancelArray"]) 
        {
        
        [self shuju];
       
        }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
}
-(void)shuju
{
    [_scroller removeFromSuperview];
    _scroller=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64)];
    _scroller.backgroundColor=COLOR;
    [self.view addSubview:_scroller];
    for (int i=0; i<[ReadyDiSignle signle].cancelArray.count; i++)
        {
        int row=i/2;
        Targets *targets=[ReadyDiSignle signle].cancelArray[i];
        NSString *string=targets.oid;
        NSString *b = [string substringFromIndex:string.length-3];
        
        self.itemButton=[[UIButton alloc] init];
        self.itemButton.frame=CGRectMake(6+(i%2)*(WIDTH/2-3),6+46*row, WIDTH/2-9, 40);
        self.itemButton.backgroundColor=[UIColor whiteColor];
        self.itemButton.layer.cornerRadius=4;
        [self.itemButton setTitle:[NSString stringWithFormat:@"%@",b] forState:UIControlStateNormal];
        [self.itemButton setTitleColor:[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1] forState:UIControlStateNormal];
        [self.itemButton setTag:110+i];
        [self.itemButton addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_scroller addSubview:self.itemButton];
        }
    _scroller.contentSize=CGSizeMake(0, CGRectGetMaxY(self.itemButton.frame));
}
-(void)turnAction:(UIButton *)sender
{
    
    Targets *targets=[ReadyDiSignle signle].cancelArray[sender.tag-110];
    [OrderMessagesignle single].oidString=targets._id;
    
    [[OrderMessagesignle single] orderMessagworking];
    
    
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
     [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"cancelArray"];
    [super viewWillDisappear:animated];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
